home *** CD-ROM | disk | FTP | other *** search
/ Dr. Windows 3 / dr win3.zip / dr win3 / WINPROGS / IWF12.ZIP / KERNEL / IMAGEIO.C < prev    next >
C/C++ Source or Header  |  1994-02-19  |  24KB  |  768 lines

  1. /*
  2. -------------------------------------------------------------------------------
  3. IMAGEIO.C  (Image Input Output)
  4.  
  5. CREATOR: Craig Muller P.Eng. 1991,1992,1993
  6.             cmuller@ccu.umanitoba.ca
  7.             Computer Vision Laboratory
  8.             University of Manitoba
  9.             Winnipeg, Manitoba. R3T 2N2
  10.             CANADA.
  11.  
  12. DESCRIPTION:
  13.  
  14. These procedures provide a general purpose interface to the image
  15. data and only have compatibility with the Windows environment.
  16.  
  17. PROCEDURES:
  18. CreateImage()    - Creates an image which can be used for storing pixel data.
  19. DestroyImage()   - Frees an image used for storing pixel data.
  20. ClearImage()     - Clears an image to zero.
  21. CopyImageData()  - Copies the pixel data from source to destination image.
  22. CopyImageRect()  - Copies a rectangle of pixels from source to destination image.
  23. CopyImageRow()   - Copies a row of pixels from source to destination image.
  24. CopyImagePal()   - Copies an image palette to a destination image
  25. DuplicateImage() - Creates a new image which is a duplicate of the original less data
  26. ReduceImage()    - Creates a new image which is a reduction of the original.
  27. ScaleImage()     - Creates a new image which scales the original into it.
  28. MirrorImage()    - Creates a new image which is a mirror of the original
  29. FlipImage()      - Creates a new image which is a flip of the original
  30. RotateImage()    - Creates a new image which is a rotation of the original
  31. GetImagePixel()  - Gets a pixel from an image at x,y and stores it in dn.
  32. SetImagePixel()  - Sets a pixel value in the source frame buffer at x,y to a value of dn.
  33. GetImageScan()   - Gets a raster line from an image at line y and stores it at dn.
  34. GetImageRow()    - Gets a row of pixels from an image and stores it at dn.
  35. PutImageRow()    - Puts a row of pixels from a buffer into an image
  36. SetImageRow()    - Sets a row of pixels to a value
  37. GetImageCol()    - Gets a column of pixels from an image and stores it at dn.
  38. PutImageCol()    - Puts a column of pixels from a buffer into an image
  39. SetImageCol()    - Sets a column of pixels to a value
  40. GetImageRect()   - Cuts a rectangle from an image and stores it in the buffer.
  41. PutImageRect()   - Pastes a rectangle from the buffer to an image.
  42. SetImageRect()   - Sets a rectangular region of pixels to a value
  43. ImageMessage()   - Puts an error message on the screen
  44. -------------------------------------------------------------------------------
  45. */
  46. #include <windows.h>
  47.  
  48. #pragma hdrstop
  49.  
  50. #include <stdio.h>
  51. #include <dos.h>
  52. #include <math.h>
  53. #include <mem.h>
  54. #include <alloc.h>
  55. #include <string.h>
  56.  
  57. #include "kernel.h"
  58.  
  59.  
  60. void CopyImageRow(IMAGE *s_image,IMAGE *d_image,int x,int y,int cb);
  61. void CopyImageRect(IMAGE *s_image,IMAGE *d_image,RECT *rs);
  62.  
  63.  
  64. /*
  65. ---------------------------------------------------------------------
  66. ImageMessage() - Puts an error message on the screen
  67. ~~~~~~~~~~
  68.  
  69. COMMENTS:
  70. ---------------------------------------------------------------------
  71. */
  72. void ImageMessage(char *sz)
  73.    {
  74.    MessageBeep(MB_ICONEXCLAMATION);
  75.     MessageBox(NULL,sz,"KERNEL ERROR (FBIO)",MB_OK | MB_ICONEXCLAMATION);
  76.    }
  77.  
  78.  
  79. /*
  80. ---------------------------------------------------------------------
  81. IMAGE *CreateImage(int w,int h)
  82. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  83. This procedure allows the user to create a memory Frame Buffer (IMAGE)
  84. in the PC memory space. There is no limit on the number of images
  85. that can be created as long as there is memory space available.
  86.  
  87. Parameters:
  88.  
  89.    w - width of image
  90.    h - height of image
  91.  
  92. Return value:
  93.  
  94.    (IMAGE *) - pointer to the new image structure.
  95.  
  96. ---------------------------------------------------------------------
  97. */
  98. IMAGE *CreateImage(int w,int h)
  99.    {
  100.    char sz[80];
  101.    int i;
  102.     IMAGE *image;
  103.  
  104.    // Allocate local memory space for the data structure for the IMAGE object.
  105.    image = (IMAGE *)LocalAlloc(LPTR,sizeof(IMAGE));
  106.    if (!image)
  107.       {
  108.       ImageMessage("Unable to Allocate Local Memory");
  109.       return(image);
  110.       }
  111.  
  112.    if (w>MAXW)
  113.       {
  114.       sprintf(sz,"Width exceeds %d pixels, image will be truncated",MAXW);
  115.       ImageMessage(sz);
  116.       w = MAXW;
  117.       }
  118.  
  119.    if (h>MAXH)
  120.       {
  121.       sprintf(sz,"Height exceeds %d pixels, image will be truncated",MAXW);
  122.       ImageMessage(sz);
  123.       h = MAXH;
  124.       }
  125.  
  126.     strcpy(image->comment,"No Comments");         // Default comment string
  127.     strcpy(image->fspec,"Untitled");              // Default file name string
  128.     image->color  = FALSE;                        // Image by default is gray
  129.     image->hres   = w;                            // Set the image width
  130.     image->vres   = h;                            // Set the image height
  131.     image->depth  = 8;                            // Only uses 8 bit images
  132.     image->palsize= 256;                          // Default palette entries used
  133.     image->gamma  = 0.5;                          // Default gamma value
  134.  
  135.     // *** IMPORTANT ***
  136.     // Even though the width of an image can be any value, when the image
  137.     // is displayed in Windows the bitmap scan must fit on a 32 bit boundary
  138.     // or else the display will appear all garbled. Scansize is used for this.
  139.     image->scansize = (w%4) ? (w/4+1)*4 : w;      // Must fit 32 bit boundary.
  140.  
  141.     SetRect(&image->rc,0,0,image->hres-1,image->vres-1);   // Set default client rectangle
  142.  
  143.     // Since the default image is gray set the input and output LUTS
  144.     // to a gray scale.
  145.     for (i=0; i<256; ++i)
  146.         {
  147.         image->ILUT[i] = i;
  148.         }
  149.     for (i=0; i<256; ++i)
  150.         {
  151.         image-> Pal[i].peRed   = i;
  152.         image-> Pal[i].peGreen = i;
  153.         image-> Pal[i].peBlue  = i;
  154.         image-> Pal[i].peFlags = 0;
  155.       image-> LUT[i].peRed   = i;
  156.       image-> LUT[i].peGreen = i;
  157.       image-> LUT[i].peBlue  = i;
  158.       image-> LUT[i].peFlags = 0;
  159.         }
  160.  
  161.    // Allocate global memory for holding the image data.
  162.    image->hData = GlobalAlloc(GMEM_MOVEABLE | GMEM_ZEROINIT,(DWORD)image->scansize*image->vres);
  163.    if (!image->hData)
  164.       {
  165.       ImageMessage("Unable to Allocate Global Memory");
  166.       image = (IMAGE *)LocalFree((LOCALHANDLE)image);
  167.       }
  168.  
  169.    return(image);                                   // Return a pointer to the new image
  170.    }
  171.  
  172.  
  173. /*
  174. ---------------------------------------------------------------------
  175. IMAGE *DestroyImage(IMAGE *image)
  176. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  177. Destroys a Frame Buffer object by first freeing any data memory and
  178. then freeing the object structure. The object is returned if it cannot
  179. be destroyed for any reason, otherwise NULL is returned if OK.
  180.  
  181. Parameters:
  182.  
  183.    image - pointer to image to be destroyed
  184.  
  185. Return value:
  186.  
  187.    (IMAGE *) - null pointer to image if successful.
  188.  
  189. ---------------------------------------------------------------------
  190. */
  191. IMAGE *DestroyImage(IMAGE *image)
  192.    {
  193.    if (image)                                       // If the image is valid
  194.       {
  195.       if (image->hData)                             // If the data is valid
  196.          image->hData = GlobalFree(image->hData);      // Free the image data
  197.       if (image->hData)                             // If the data is still valid
  198.          {                                       // It was unable to free it
  199.          ImageMessage("Unable to Free Global Memory");
  200.          return(image);
  201.          }
  202.       image = (IMAGE *)LocalFree((LOCALHANDLE)image);     // Free the local structure
  203.       if (image)                                    // If it is still there
  204.          {                                       // It was unable to free it
  205.          ImageMessage("Unable to Free Local Memory");
  206.          return(image);
  207.          }
  208.       }
  209.  
  210.    return(image);                                   // Return image pointer
  211.    }
  212.  
  213. /*
  214. -------------------------------------------------------------------------------
  215. IMAGE *DuplicateImage(IMAGE *image)
  216. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  217. This procedure allows the user to duplicate a image structure
  218. in the PC memory space. There is no limit on the number of memory FBs
  219. that can be created as long as there is memory space available.  The orginal
  220. image is not destroyed.
  221.  
  222. Parameters:
  223.  
  224.    image - pointer to source image
  225.  
  226. Return value:
  227.  
  228.